define(["jquery","bbcare","comm/datatable/datatable","bld/app/course/courseList.servers","bld/app/course/courseList.ui"],function($,$bb,$datatable,$servers,$ui) {
    var courseList = {};
    var className = "courseListModal";
        
    /**
     * 添加疗程成功后的回调函数
     */
    var ajaxAddCallBack = function(data) {
        $('.' + className).modal('hide');
        $datatable.table.api().ajax.reload(); // 出发dt的重新加载数据的方法
    }

    /**
     * 查询疗程库分类
     */
    var ajaxCourseClassCallBack = function(data){
        if( data.rows.length > 0 ){
            var courseName = $("#tpl_courseName").val();
            if( courseName == "" ){
                $bb.toastShow("请输入疗程名称！"); 
            }else{
                var scheme_id = $("#tpl_classId").val();
                var courseIntro = $("#tpl_courseIntro").val();
                var courseType = $("#tpl_courseType").val();
                var groupValue = $("#tpl_group").val();
                var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
                var channelId = $bb.getChannelId();
                var params = {
                    "oprInfo":{
                        "opCode": "2050",
                        "opType": "3",
                        "opNote": "【" + doctorName + "】医生添加疗程【" + courseName + "】",
                        "channelType": "2",
                        "channelId": channelId
                    },
                    "course":{
                        "courseName": courseName,
                        "classId": data.rows[0].id,
                        "courseIntro": courseIntro,
                        "scheme_id": scheme_id
                    },
                    "courseVersion":{
                        "courseType": courseType,
                        "groupType": "0",
                        "groupValue": groupValue
                    }
                };
                $servers.ajaxAdd(params,ajaxAddCallBack);
            }
        }else{
            $bb.toastShow("该系统下没有配置疗程分类，请配置！");
        }
    }
        
    /**
     * 添加疗程
     */
    var onAdd = function() {
        $servers.ajaxCourseClass(ajaxCourseClassCallBack);
    }

    /**
     * 添加疗程
     */
    var onAddClick = function() {
        var datas = $(this).data("datas");
        $bb.modal({
            "className": className,
            "tit": "添加疗程",
        }, function() {
            $ui.add({
                "dom": $("." + className + " .modal-body"),
                "schemeClass":datas.schemeClass
            });
            $("." + className).modal("show");
            //添加疗程
            $("#add").unbind("click");
            $("#add").bind("click", onAdd);
        });
    }

    /**
     * 获取所有方案库回调函数
     */
    var ajaxQueryAllScheme = function(data) {
        // 方案分类
        $ui.queryAllScheme({
            "id": "classify",
            "list": data.schemeClassInfos
        });
        // datatable
        var id = $(".layout-bottom table").attr("id");
        $datatable.init({
            "id": id,
            "options": $servers.getOptions()
        });
        // 添加疗程
        $("#addBtn").unbind("click");
        $("#addBtn").data("datas",{"schemeClass":data.schemeClassInfos});
        $("#addBtn").click(onAddClick);
    }

    courseList.init = function() {
        $servers.ajaxQueryAllScheme(ajaxQueryAllScheme);
    };

    return courseList;
});